<%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8" contentType="text/html; charset=utf-8" %>
<%@include file="/quick/include/common.jsp"%>
<div id='productTable_win' style='display:none'></div>
<div id='productTable_html' style='display:none'></div>
<script>
function f_addProduct(noteProductTable,_afterConfirm){
  var storeStock=new Ext.data.Store({//仓库数据源
    proxy:new Ext.data.HttpProxy({url:g_rootPath+"ajax/stock/findAllStocks.html"}),
    reader:new Ext.data.ArrayReader({},[
      {name:'stockNo'},
      {name:'stockName'}
    ]) 
  });
  storeStock.load();
  
  var comboStock=new Ext.form.ComboBox({//仓库下拉框
    store:storeStock,
    fieldLabel:'仓库',
    emptyText:'请选择',
    mode:'local',
    triggerAction:'all',
    valueField:'stockNo',
    displayField:'stockName',
    readOnly:true
  });
  
  var storeLocation=new Ext.data.Store({//库位数据源
    proxy:new Ext.data.HttpProxy({url:g_rootPath+"ajax/stock/findLocationsByStock.html"}),
    reader:new Ext.data.ArrayReader({},[
      {name:'locationNo'},
      {name:'locationName'}
    ])
  });
  var comboLocation=new Ext.form.ComboBox({//库位下拉框
    store:storeLocation,
    fieldLabel:'库位',
    emptyText:'请选择',
    mode:'local',
    triggerAction:'all',
    valueField:'locationNo',
    displayField:'locationName',
    readOnly:true
  });
  
  var storeVender=new Ext.data.Store({//供应商数据源
    proxy:new Ext.data.HttpProxy({url:g_rootPath+"ajax/vender/findAllVenders.html"}),
    reader:new Ext.data.ArrayReader({},[
      {name:'venderNo'},
      {name:'venderCname'}
    ])
  });
  storeVender.load();
  
  var comboVender=new Ext.form.ComboBox({//供应商下拉框
    store:storeVender,
    fieldLabel:'供应商',
    emptyText:'请选择',
    mode:'local',
    triggerAction:'all',
    valueField:'venderNo',
    displayField:'venderCname',
    readOnly:true
  });
  
  
  
  comboStock.on('select',function(comboBox){
    var stockNo=comboBox.getValue();
    storeLocation.load({params:{'stockNo':stockNo}});
  });

  var stockLocationForm=new Ext.form.FormPanel({
    region:'north',
    labelAlign:'right',
    labelWidth:60,
    frame:true,
    split:true,
    collapsible:true,
    title: "仓库、库位,产品供应商",
    width:650,
     height:g_height*0.15,
    items:[{
      layout:'column',
      items:[{
          columnWidth:1/3,
          layout:'form',
          items:[comboStock]
        },{
          columnWidth:1/3,
          layout:'form',
          items:[comboLocation]
        },{
          columnWidth:1/3,
          layout:'form',
          items:[comboVender]
        }
      ]
    }]
  });



  Ext.get('productTable_win').dom.innerHTML="<div id='tree_div' style='display:block'></div>"; 
  var tree = new Ext.tree.TreePanel({
        el:'tree_div',
	    region:'west',
	    width:200,
	    loader: new Ext.tree.TreeLoader({dataUrl: g_rootPath+'product/getProductLevelTree.html'})
	});
  var root = new Ext.tree.AsyncTreeNode({id:'0',text:'产品等级',productId:'0',icon:g_rootPath+'extjs/resources/images/default/grid/hmenu-lock.png'});
  tree.setRootNode(root);
  
  var htmlDiv=Ext.get("productTable_html").dom;
  var productTable='T_PRODUCT';
  tree.on('click',function(node){
    globalScope._model({tableName:productTable,REFID:node.attributes.productId},function(responseText){
	   htmlDiv.innerHTML=responseText;
	   var span=Ext.get(productTable+"_columns").dom;
	   var columns=Ext.decode(span.innerText);
	   var productGrid= new Ext.quick.QuickGrid(productTable, {
	     id:'c_grid',
	     collapsible:true,
	     split:true,
	     stripeRows: true, 
	     enableDragDrop:false,
	     title:'产品',
	     clicksToEdit:1,
	     'remove':true,
	     height:g_height*85,
	     'smConfig':{}
	   },columns);
	   
	   
	   globalScope.grid.add(productTable,productGrid);
	   
	   var p=Ext.getCmp('c_product');
	   p.remove('c_grid');
	   p.add(productGrid);
	   p.doLayout();
   });     
  });
  

  var selectProductWin= new Ext.Window({
    id:'c_win2',
    el:'productTable_win',
    layout:'border',
    title:'选择产品',
    width:1000,
    height:g_height*1.2,
    closeAction:'hide',
    buttonAlign:'center',
    items: [tree,stockLocationForm,{id:'c_product',region:'center',layout:'fit',items:[{id:'c_grid'}] }],
    buttons: [{
	        text:'<fmt:message key="button.conf"/>',
	        handler:function(){
	            var productGrid=globalScope.grid.get(productTable);
	            var m=null;
                if(Ext.isEmpty(productGrid)==false){
                   m=productGrid.getSelectionModel().getSelections();     
                }
			    if(Ext.isEmpty(productGrid)||m.length==0){
			      Ext.Msg.confirm('title','没有选择产品,确认吗',function(btn){
                       if(btn=='yes'){
                         selectProductWin.hide();
                       }
                     });
                     return;
			    }
		        var recordData={STOCK_NO:comboStock.getValue(),LOCAL_NO:comboLocation.getValue(),VENDER_NO:comboVender.getValue()};
			    if(typeof(_afterConfirm)=='function'){
			      _afterConfirm(m,recordData);
			    }
			    selectProductWin.hide();
            }
	      },{
	        text:'<fmt:message key="button.close"/>',
	        handler:function(){selectProductWin.hide();}
	     }]
	});  
   selectProductWin.render(document.body);  
   selectProductWin.show(); 	 

}

</script>
